package com.hdax.controller;

import com.hdax.entity.User;
import com.hdax.service.TongjiService;
import com.hdax.vo.DriverVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/tongji")
public class TongjiController {
    @Autowired
    private TongjiService service;

    @GetMapping("/openPage1")
    public ModelAndView openPage(){
        return new ModelAndView("tongji/driverCount");
    }

    @PostMapping("/findDriveCount")
    public Map<String,List> findDriveCount(){
        List<String> seriesList = service.findSeries();//每个汽车类型
        List<User> userList = service.findUserAll();
        List<DriverVo> driverVoList = new ArrayList<>();
        for (String s : seriesList) {
            DriverVo driverVo = new DriverVo();
            driverVo.setName(s);
            List<Integer> counts = new ArrayList<>();
            List<Integer> carIds = service.findCarId(s);//车型对应的所有汽车编号
            for (User user : userList) {
                Map map = new HashMap();
                map.put("userId",user.getId());
                map.put("list",carIds);
                counts.add(service.findCusDriveCountByUserIdAndCarSeries(map));
            }
            driverVo.setCounts (counts);
            driverVoList.add(driverVo);
        }
        Map<String,List> listMap = new HashMap<>();
        listMap.put("driveList",driverVoList);
        listMap.put("userList",userList);
        return listMap;
    }

    @PostMapping("/findBuyCarCount")
    public  List<Map> findBuyCount(){
        return service.findBuyCarCount();
    }


    @GetMapping("/openPage2")
    public ModelAndView openPage2(){
        return new ModelAndView("tongji/customerCount");
    }

    @PostMapping("/findCustomerCount")
    public List<Map> findCustomerCount(){
        return service.findCustomerCount();
    }
}
